package com.demo.tx.controller;

import com.demo.tx.seata.SeataService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 该Controller是测试XA模式，启动参数必须加上 -Dserver.port=8864 -Dspring.profiles.active=xa
 * @author cyj
 */
@Slf4j
@RestController
public class SeataXAController {

    @Resource
    private SeataService seataService;

    @GlobalTransactional(rollbackFor = Exception.class)
    @GetMapping("/insert/{userId}")
    public void insert(@PathVariable("userId") Long userId) {
        log.info("insert userId:{}", userId);
        seataService.insert(userId);
    }

    @GetMapping("/insertError/{userId}")
    public void insertError(@PathVariable("userId") Long userId) {
        log.info("insertError userId:{}", userId);
        throw new RuntimeException("SeataXAController.insertError 异常");
    }
}
